package com.linuxgt.mobile;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * User: LIUTENG
 * Date: 13-5-2
 * Time: 下午5:10
 * To change this template use File | Settings | File Templates.
 */
public class Xml {
    public static Document xmlResult(String body) {
        //TODO xml转换
        DocumentBuilderFactory domfac= DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder dombuilder=domfac.newDocumentBuilder();
            InputStream is=new FileInputStream("src/main/resources/wx/get/wxtext.xml");
            Document doc= dombuilder.parse(is);
            Element root=doc.getDocumentElement();
            NodeList infos=root.getChildNodes();
            System.out.println(doc.getElementById("ToUserName"));
            if(infos!=null){
                int infoLength=infos.getLength();
                for(int i=0;i<infoLength;i++){

                    Node info=infos.item(i);
                    if(info.getNodeType()==Node.ELEMENT_NODE){
                        System.out.println(info.getChildNodes().item(0).getNodeValue());
                        System.out.println(info.getChildNodes().item(0).getNodeName());
                        System.out.println(info.getChildNodes().item(0).getNodeType());
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SAXException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return null;  //To change body of created methods use File | Settings | File Templates.
    }

    public static void main(String[] args) {
        xmlResult("a");
    }
}
